Ext.ns('cn.hls.web.action.biz.shipper');
var shipperForm = cn.hls.web.action.biz.shipper.shipperForm,photoWin,photoPanel,safetyCardwin,safetyCardPhotoPanel,oldImgId,safetycard_oldId,safetyCardIndex=0;
var allow_save=true;//定义是否允许添加记录操作
var allow_receive_save=true;//定义是否允许添加收货记录操作
var all_send_save=true;//定义是否允许添加发货记录操作
var goods_trIndex=[];//定义货物表格tr序号
var send_trIndex=[];//定义发货表格tr序号
var receive_trIndex=[];//定义收货表格tr序号
 shipperForm = function() {
	var uploadPanel,win;
   return { 
      init : function() {
    	this.initRender();
      },
      //个人图片显示
      viewPhoto:function(photo,imgId){
    	  if(!photoPanel||oldImgId!=imgId){
    		  oldImgId=imgId;
    		  shipperForm.createPhotodPanel(imgId);
    		  photoWin=undefined;
    	  }
    	  if(!photoWin){
    		  photoWin = new ExtWin({
    			width : 400,
  				height : 480,
  				title : '<div class="panelTitleCss">个人照片</div>',
    		  	items:[photoPanel],
    		  	buttons : [{
					text : '关闭',
					iconCls : 'deleteIcon',
					handler : function() {
						$("#"+imgId).hide();
						photoWin.hide();
					}
				}]
    		  });
    	  }
    	  $("#"+imgId).show();
    	  photoWin.show();
      },
      createPhotodPanel:function(id){
    	  photoPanel=new Ext.Panel({
    		  layout : 'fit',
    		  items: [{
		        	padding : 5,
					bodyStyle:'overflow-y:auto;overflow-x:hidden;',
					contentEl : id
		        }]
    	  });
    	  return photoPanel;
      },
      //应急方案图片
      openSafetyCardView:function(id,filePath){
    	  safetyCardIndex=0;
    	  var files=filePath.split(',')
    	  if(!safetyCardPhotoPanel||safetycard_oldId!=id){
    		  safetycard_oldId=id;
    		  shipperForm.createSafetCardPhotodPanel(id);
    		  safetyCardwin=undefined;
    	  }
    	  if(!safetyCardwin){
    		  safetyCardwin = new ExtWin({
    			width : 700,
  				height : 650,
  				title : '<div class="panelTitleCss">应急方案</div>',
    		  	items:[safetyCardPhotoPanel],
    		  	buttons : [
    		  	    {text:'上一张',
    		  	     id:'btn_pre',
    		  	     iconCls:'x-tbar-page-prev',
    		  	     handler:function(){
    		  	    	 if(safetyCardIndex>0){
    		  	    		 safetyCardIndex--;
			  	    		 var extStart = files[safetyCardIndex].lastIndexOf(".");
				    	     var ext = files[safetyCardIndex].substring(extStart, files[safetyCardIndex].length).toUpperCase();
				    	     if (ext != ".PDF"&&ext != ".DOC"&&ext != ".DOCX"){//显示图片
				    	    	 $("#safetyCard_photo_img_"+id).attr('src',cfg.cxt+'/upload/'+files[safetyCardIndex]);
				    	    	 $("#safetyCard_photo_img_"+id).show();
				    	    	 $("#safetyCard_attach_sp_"+id).hide();
				    	     }else{
				    	    	 $("#safetyCard_attach_sp_"+id).html('<a target="_blank" href="'+cfg.cxt+'/upload/'+files[safetyCardIndex]+'">附件：'+files[safetyCardIndex]+'</a>');
				    	    	 $("#safetyCard_photo_img_"+id).attr('src',cfg.cxt+'/upload/'+files[safetyCardIndex]);
				    	    	 $("#safetyCard_photo_img_"+id).hide();
				    	    	 $("#safetyCard_attach_sp_"+id).show();
				    	     }
    		  	    	 }
    		  	     }
    		  	    },{
    		  	    	text:'下一张',
    		  	    	iconCls:'x-tbar-page-next',
    		  	    	id:'btn_next',
    		  	    	handler:function(){
    		  	    		 if(safetyCardIndex<(files.length-1)){
    		  	    			safetyCardIndex++;
    		  	    			 var extStart = files[safetyCardIndex].lastIndexOf(".");
    				    	     var ext = files[safetyCardIndex].substring(extStart, files[safetyCardIndex].length).toUpperCase();
    				    	     if (ext != ".PDF"&&ext != ".DOC"&&ext != ".DOCX"){//显示图片
    				    	    	 $("#safetyCard_photo_img_"+id).attr('src',cfg.cxt+'/upload/'+files[safetyCardIndex]);
    				    	    	 $("#safetyCard_photo_img_"+id).show();
    				    	    	 $("#safetyCard_attach_sp_"+id).hide();
    				    	     }else{
    				    	    	 $("#safetyCard_attach_sp_"+id).html('<a target="_blank" href="'+cfg.cxt+'/upload/'+files[safetyCardIndex]+'">附件：'+files[safetyCardIndex]+'</a>');
    				    	    	 $("#safetyCard_photo_img_"+id).hide();
    				    	    	 $("#safetyCard_attach_sp_"+id).show();
    				    	     }
        		  	    	 }
    		  	    	}
    		  	    },
    		  	    {
					text : '关闭',
					iconCls : 'deleteIcon',
					handler : function() {
						$("#safetyCard_photo_img_"+id).attr('src','');
						$("#safetyCard_photo_img_"+id).hide();
						$("#safetyCard_attach_sp_"+id).html('');
						$("#safetyCard_attach_sp_"+id).hide();
						safetyCardwin.hide();
					}
				}]
    		  });
    	  }
    	  if(files.length==1){
    		  Ext.getCmp('btn_pre').setVisible(false);//设置不可见
    		  Ext.getCmp('btn_next').setVisible(false);//设置不可见
    	  }else{
    		  Ext.getCmp('btn_pre').setVisible(true);//设置不可见
    		  Ext.getCmp('btn_next').setVisible(true);//设置不可见
    	  }
    	  var extStart = files[0].lastIndexOf(".");
 	      var ext = files[0].substring(extStart, files[0].length).toUpperCase();
 	     if (ext != ".PDF"&&ext != ".DOC"&&ext != ".DOCX"){//显示图片
	    	  $("#safetyCard_photo_img_"+id).attr('src',cfg.cxt+'/upload/'+files[0]);
	    	  $("#safetyCard_photo_img_"+id).show();
	    	  $("#safetyCard_attach_sp_"+id).hide();
 	     }else{
 	    	$("#safetyCard_attach_sp_"+id).html('<a target="_blank" href="'+cfg.cxt+'/upload/'+files[safetyCardIndex]+'">附件：'+files[safetyCardIndex]+'</a>');
	    	 $("#safetyCard_photo_img_"+id).hide();
	    	 $("#safetyCard_attach_sp_"+id).show();
 	     }
    	  safetyCardwin.show();
      },
      /**应急方案panel**/
      createSafetCardPhotodPanel:function(id){
    	  safetyCardPhotoPanel=new Ext.Panel({
    		  layout : 'fit',
    		  items: [{
		        	padding : 5,
					bodyStyle:'overflow-y:auto;overflow-x:hidden;',
					contentEl : 'sp_content_'+id
		        }]
    	  });
    	  return safetyCardPhotoPanel;
      },
      /**企业基本信息下拉框 store**/
      createCompanyStore:function(){
     	 return new Ext.data.Store({
     		 	autoLoad:true,//自动加载   
 				proxy : new Ext.data.HttpProxy({ url : cfg.cxt+'/company/loadCompanyOfShipper.action' }),
 				reader : new Ext.data.JsonReader({idProperty : 'id'},
 				["shipperId","name"]),
 				listeners : { 
 					loadexception : bangni.storeLoadError
 				}
 			});
      },
      /**货物基本信息下拉框 store**/
      createGoodsStore:function(){
     	 return new Ext.data.Store({
     		 	autoLoad:true,//自动加载   
 				proxy : new Ext.data.HttpProxy({ url : cfg.cxt+'/goods/loadGoods.action' }),
 				reader : new Ext.data.JsonReader({idProperty : 'id'},
 				["id","name","category.internatCode","code","safetyCard"]),
 				listeners : { 
 					loadexception : bangni.storeLoadError
 				}
 			});
      },
      /**发、收货站点下拉框store**/
      createSiteStore:function(type){
    	  return new Ext.data.Store({
   		 	autoLoad:true,//自动加载   
				proxy : new Ext.data.HttpProxy({ url : cfg.cxt+'/site/loadSiteList.action' }),
				reader : new Ext.data.JsonReader({idProperty : 'id'},
				["id","name","provinceName","cityName","areaName","contractor","contractPhone","contractPhoto","address"]),
				baseParams:{'type':type,'company.carryCompanyId':carryCompanyId==''?undefined:carryCompanyId},
				listeners : { 
					loadexception : bangni.storeLoadError
				}
			});
      },
      /**车辆信息下拉框store**/
      createCarStore:function(){
      	 return new Ext.data.Store({
      		 	autoLoad:true,//自动加载   
  				proxy : new Ext.data.HttpProxy({ url : cfg.cxt+'/car/loadCar.action' }),
  				reader : new Ext.data.JsonReader({idProperty : 'id'},
  				["id","licenseNum","licenseNum2","carCategory.name","driver","escort","carryCapacity"]),
  				baseParams:{'company.id':carryCompanyId==''?undefined:carryCompanyId},
  				listeners : { 
  					loadexception : bangni.storeLoadError
  				}
  			});
       },
       
       /**驾驶员信息下拉框列表**/
       createDriverStore:function(){
    	   return new Ext.data.Store({
     		 	autoLoad:true,//自动加载   
 				proxy : new Ext.data.HttpProxy({ url : cfg.cxt+'/member/loadMemberList.action' }),
 				reader : new Ext.data.JsonReader({idProperty : 'id'},
 				["id","name","gender","idno","photo","contractPhone","driverLiceseNum","superCargoNum"]),
 				baseParams:{'type':1,'company.id':carryCompanyId==''?undefined:carryCompanyId},
 				listeners : { 
 					loadexception : bangni.storeLoadError
 				}
 			});
       },
       
       /**押运员信息下拉框列表**/
       createEscortStore:function(){
    	   return new Ext.data.Store({
     		 	autoLoad:true,//自动加载   
 				proxy : new Ext.data.HttpProxy({ url : cfg.cxt+'/member/loadMemberList.action' }),
 				reader : new Ext.data.JsonReader({idProperty : 'id'},
 				["id","name","gender","idno","photo","contractPhone","driverLiceseNum","superCargoNum"]),
 				baseParams:{'type':2,'company.id':carryCompanyId==''?undefined:carryCompanyId},
 				listeners : { 
 					loadexception : bangni.storeLoadError
 				}//,paramNames:{'type':2}
    	   		
 			});
       },
       
      initRender: function(){
    	  //托运单时间
    	  var shipTime=Ext.get('hid_shipTime').dom.value;
    	  //装货时间
    	  var loadGoodsTime=Ext.get('hid_loadGoodsTime').dom.value;
    	  //卸货时间
    	  var unloadGoodsTime=Ext.get('hid_unloadGoodsTime').dom.value;
    	  new Ext.form.DateField({
				id : 'dtshipTime',
				width : 170,
				editable : false,
				format : "Y-m-d",
				name : 'shipTime',
				renderTo : 'shipTime',
				allowBlank : false,
				value:Ext.isEmpty(shipTime)?new Date():new Date(shipTime.substr(0,10))
			});
    	  new Ext.ux.form.DateTimeField ({
				id : 'dtloadGoodsTime',
				width : 170,
				editable : false,
				dateFormat: 'Y-m-d',
			    timeFormat: 'H:i:s',
			    xtype: 'datetimefield', 
				name : 'loadGoodsTime',
				renderTo : 'loadGoodsTime',
				allowBlank : false,
				value:Ext.isEmpty(loadGoodsTime)?new Date():loadGoodsTime.substr(0,19)
			});
    	  new Ext.ux.form.DateTimeField({
				id : 'dtunloadGoodsTime',
				width : 170,
				editable : false,
				format : "Y-m-d H:i:s",
				name : 'unloadGoodsTime',
				renderTo : 'unloadGoodsTime',
				allowBlank : false,
				value:Ext.isEmpty(unloadGoodsTime)?new Date():unloadGoodsTime.substr(0,19)
			});
          
    	  new Ext.form.ComboBox({ //默认过滤参数名为query     
              store: shipperForm.createCompanyStore(),    
              hiddenName:'name',
              id:'shipper',
              width : 200,
              mode : 'local', //设置为local,则加载的本地数据     
              typeAheadDelay:250,//默认延时查询250     
              triggerAction : 'all', //每次下拉均显示全部选项       
              selectOnFocus:true,
              forceSelection: true,// 用户必须点击选择不能随意输入  
              typeAhead: true,// 自动提示并补充列出相似的选项  
              valueField : 'shipperId', //下拉框显示信息对应的值     
              displayField : 'name', //下拉框显示信息     
              loadingText : '正在加载信息',      
              selectOnFocus : true, //用户不能自己输入,只能选择列表中有的记录     
              minChars:2,  //设置填充几个字节就去查一次,默认为4个字节      
              renderTo : 'sp_shipper',
              listeners:{'select':function(combo,record,index){
            	   $("#shipperId").val(record.data.shipperId);
             }
    	  }
    	  });
	      $("#shipper").attr('datatype',"*");
	      //编辑时设置下拉框的默认值
	      if($("#shipperId").val()!=''){
	    	  var companyName=$("#companyName").val();
	    	  $("#shipper").val(companyName);//下拉框设置值
	      }
	      
	      /**发货方下拉框设置**/
	      for(var i=0;i< $("#sendSite_Tab tr").length-2;i++){//头部存在2行合并
	    	  var k=(++i);
	    	  i--;//恢复到初始状态
		      new Ext.form.ComboBox({ //默认过滤参数名为query     
	              store: shipperForm.createSiteStore(2),    
	              hiddenName:'name',
	              id:'sendSite_'+k,
	              width : 170,
	              mode : 'local', //设置为local,则加载的本地数据     
	              typeAheadDelay:250,//默认延时查询250     
	              triggerAction : 'all', //每次下拉均显示全部选项       
	              selectOnFocus:true,
	              forceSelection: true,// 用户必须点击选择不能随意输入  
	              typeAhead: true,// 自动提示并补充列出相似的选项  
	              valueField : 'id', //下拉框显示信息对应的值     
	              displayField : 'name', //下拉框显示信息     
	              loadingText : '正在加载信息',      
	              selectOnFocus : true, //用户不能自己输入,只能选择列表中有的记录     
	              minChars:2,  //设置填充几个字节就去查一次,默认为4个字节      
	              renderTo : 'sp_sendSite_'+k,
	              listeners:{'select':function(combo,record,index){
	            	   $("#sendSiteId_"+k).val(record.data.id);
	            	   $("#hid_sendSiteName_"+k).val(record.data.name);
	            	   $("#sendProvinceName_"+k).val(record.data.provinceName);
	            	   $("#sendCityName_"+k).val(record.data.cityName);
	            	   $("#sendAreaName_"+k).val(record.data.areaName);
	            	   $("#send_contractor_"+k).val(record.data.contractor);
	            	   $("#send_contractPhone_"+k).val(record.data.contractPhone);
	            	   $("#loadAddress_"+k).val(record.data.address);
	            	   if($.trim(record.data.contractPhoto)!=''){
	            		   var html='<a href="javascript:void(0);" onclick="javascript:shipperForm.viewPhoto(\''+record.data.contractPhoto+'\',\'send_photo_img_'+k+'\')"> 查看照片</a>';
	            		   html+='<img id="send_photo_img_'+k+'" src="'+cfg.cxt+'/upload/'+record.data.contractPhoto+'" style="display:none;"/>';
	            		   $("#send_photo_sp_"+k).html(html);
	            	   }
	             }
	    	  }
	    	  });
		      $("#sendSite_"+k).attr('datatype',"*");
		      //编辑时设置下拉框的默认值
		      if($("#sendSiteId_"+k).val()!=''){
		    	  var sendSiteName=$("#hid_sendSiteName_"+k).val();
		    	  $("#sendSite_"+k).val(sendSiteName);//下拉框设置值
		      }
		      
	      }
	      
	      /**收货方下拉框设置**/
	      for(var i=0;i< $("#receiveSite_Tab tr").length-2;i++){//头部存在2行合并
	    	  var k=(++i);
	    	  i--;//恢复到初始状态
		      new Ext.form.ComboBox({ //默认过滤参数名为query     
	              store: shipperForm.createSiteStore(1),    
	              hiddenName:'name',
	              id:'receiveSite_'+k,
	              width : 170,
	              mode : 'local', //设置为local,则加载的本地数据     
	              typeAheadDelay:250,//默认延时查询250     
	              triggerAction : 'all', //每次下拉均显示全部选项       
	              selectOnFocus:true,
	              forceSelection: true,// 用户必须点击选择不能随意输入  
	              typeAhead: true,// 自动提示并补充列出相似的选项  
	              valueField : 'id', //下拉框显示信息对应的值     
	              displayField : 'name', //下拉框显示信息     
	              loadingText : '正在加载信息',      
	              selectOnFocus : true, //用户不能自己输入,只能选择列表中有的记录     
	              minChars:2,  //设置填充几个字节就去查一次,默认为4个字节      
	              renderTo : 'sp_receiveSite_'+k,
	              listeners:{'select':function(combo,record,index){
	            	   $("#receiveSiteId_"+k).val(record.data.id);
	            	   $("#hid_receiveSiteName_"+k).val(record.data.name);
	            	   $("#receiveProvinceName_"+k).val(record.data.provinceName);
	            	   $("#receiveCityName_"+k).val(record.data.cityName);
	            	   $("#receiveAreaName_"+k).val(record.data.areaName);
	            	   $("#receive_contractor_"+k).val(record.data.contractor);
	            	   $("#receive_contractPhone_"+k).val(record.data.contractPhone);
	            	   $("#unloadAddress_"+k).val(record.data.address);
	            	   if($.trim(record.data.contractPhoto)!=''){
	            		   var html='<a href="javascript:void(0);" onclick="javascript:shipperForm.viewPhoto(\''+record.data.contractPhoto+'\',\'receive_photo_img_'+k+'\')"> 查看照片</a>';
	            		   html+='<img id="receive_photo_img_'+k+'" src="'+cfg.cxt+'/upload/'+record.data.contractPhoto+'" style="display:none;"/>';
	            		   $("#safetCard_sp_"+k).html(html);
	            	   }
	             }
	    	  }
	    	  });
		      $("#receiveSite_"+k).attr('datatype',"*");
		    //编辑时设置下拉框的默认值
		      if($("#receiveSiteId_"+k).val()!=''){
		    	  var receiveSiteName=$("#hid_ReceiveSiteName_"+k).val();
		    	  $("#receiveSite_"+k).val(receiveSiteName);//下拉框设置值
		      }
	      }
	      /**货物信息下拉框初始化**/
	      for(var i=0;i< $("#goods_Tab tr").length-2;i++){//头部存在2行合并
	    	  var k=(++i);
	    	  i--;//恢复到初始状态
		      new Ext.form.ComboBox({ //默认过滤参数名为query     
	              store: shipperForm.createGoodsStore(),    
	              hiddenName:'name',
	              id:'goods_'+k,
	              width : 170,
	              mode : 'local', //设置为local,则加载的本地数据     
	              typeAheadDelay:250,//默认延时查询250     
	              triggerAction : 'all', //每次下拉均显示全部选项       
	              selectOnFocus:true,
	              forceSelection: true,// 用户必须点击选择不能随意输入  
	              typeAhead: true,// 自动提示并补充列出相似的选项  
	              valueField : 'id', //下拉框显示信息对应的值     
	              displayField : 'name', //下拉框显示信息     
	              loadingText : '正在加载信息',      
	              selectOnFocus : true, //用户不能自己输入,只能选择列表中有的记录     
	              minChars:2,  //设置填充几个字节就去查一次,默认为4个字节      
	              renderTo : 'sp_goods_'+k,
	              listeners:{'select':function(combo,record,index){
	            	  if(safetyCardwin){
	            		  safetyCardwin=undefined;
	            	  }
	            	  if(safetyCardPhotoPanel){
	            		  safetyCardPhotoPanel=undefined;
	            	  }
	            	   safetyCardIndex=0;
	            	   $("#goodsId_"+k).val(record.data.id);
	            	   $("#internatCode_"+k).val(record.data["category.internatCode"]);
	              	   $("#code_"+k).val(record.data.code);
	              	   var safetyCardObj=record.data.safetyCard;
	              	   //if($.trim(record.data["safetyCard.path"])!=''){
	              	   if(safetyCardObj!=null){
	            		   var html='<a href="javascript:void(0);" onclick="javascript:shipperForm.openSafetyCardView('+k+',\''+safetyCardObj.path+'\')">应急方案</a><span id="sp_content_'+k+'">';
	            		   html+='<img id="safetyCard_photo_img_'+k+'" src="'+cfg.cxt+'/upload/'+safetyCardObj.path+'" style="display:none;width:100%;height:auto;"/>';
	            		   html+='<span id="safetyCard_attach_sp_'+k+'" style="display:none;margion-top:20px;padding:15px;"/>';
	            		   html+='</span>';
	            		   if(safetyCardObj.infoPath!=null && safetyCardObj.infoPath!=''){
	            			   html+='<span style="margin-left:5px;"><a href="'+cfg.cxt+'/upload/'+safetyCardObj.infoPath+'" target="_blank">安全说明书</a></span>';
	            		   }
	            		   $("#safetCard_sp_"+k).html(html);
	            	   }
	             }
	    	  }
	    	  });
		      $("#goods_"+k).attr('datatype',"*");
		      //编辑时设置下拉框的默认值
		      if($("#goodsId_"+k).val()!=''){
		    	  var goodsName=$("#hid_goodsName_"+k).val();
		    	  $("#goods_"+k).val(goodsName);//下拉框设置值
		      }
	      }
	      
	      new Ext.form.ComboBox({ //默认过滤参数名为query     
              store: shipperForm.createCarStore(),    
              hiddenName:'car.licenseNum',
              id:'car',
              width : 200,
              mode : 'local', //设置为local,则加载的本地数据     
              typeAheadDelay:250,//默认延时查询250     
              triggerAction : 'all', //每次下拉均显示全部选项       
              selectOnFocus:true,
              forceSelection: true,// 用户必须点击选择不能随意输入  
              typeAhead: true,// 自动提示并补充列出相似的选项  
              valueField : 'licenseNum', //下拉框显示信息对应的值     
              displayField : 'licenseNum', //下拉框显示信息     
              loadingText : '正在加载信息',      
              selectOnFocus : true, //用户不能自己输入,只能选择列表中有的记录     
              minChars:2,  //设置填充几个字节就去查一次,默认为4个字节      
              renderTo : 'sp_car',
              listeners:{'select':function(combo,record,index){
            	   $("#carId").val(record.data.id);
            	   $("#licenseNum2").val(record.data.licenseNum2);
            	   $("#carType").val(record.data["carCategory.name"]);
            	   $("#carryCapacity").val(record.data.carryCapacity);
            	   var driver=record.data.driver;
            	   var escort=record.data.escort;
            	   //驾驶员信息
            	   if(driver!=null &&driver!=undefined&& driver!=''){
            		   $("#hid_driver_id").val(driver.id);
                	   $("#hid_driver_name").val(driver.name);
                	   $("#comb_driver").val(driver.name);//下拉框设置值
                	   $("#driver_idno").val(driver.idno);
                	   $("#driver_driverLiceseNum").val(driver.driverLiceseNum)
                	   $("#driver_superCargoNum").val(driver.superCargoNum);
                	   $("#driver_contractPhone").val(driver.contractPhone);
                	   if($.trim(driver.photo)!=''){
                		   $("#driver_photo").attr('src',cfg.cxt+'/upload/'+driver.photo);
                	   }
            	   }
            	   //押运员信息
            	   if(escort!=null && escort!=undefined&& escort!=''){
            		   $("#hid_escort_id").val(escort.id);
                	   $("#hid_escort_name").val(escort.name);
                	   $("#comb_escort").val(escort.name);
                	   $("#escort_idno").val(escort.idno);
                	   $("#escort_superCargoNum").val(escort.superCargoNum);
                	   $("#escort_contractPhone").val(escort.contractPhone);
                	   if($.trim(escort.photo)!=''){
                		   $("#escort_photo").attr('src',cfg.cxt+'/upload/'+escort.photo);
                	   }
            	   }
             }
    	  }
    	  });
	     $("#car").attr('datatype',"*");
	      //编辑时设置下拉框的默认值
	      if($("#carId").val()!=''){
	    	  var licenseNum=$("#hid_licenseNum").val();
	    	  $("#car").val(licenseNum);//下拉框设置值
	      }
	      //驾驶员下拉框初始化
	      new Ext.form.ComboBox({ //默认过滤参数名为query     
              store: shipperForm.createDriverStore(),    
              hiddenName:'name',
              id:'comb_driver',
              width : 200,
              mode : 'local', //设置为local,则加载的本地数据     
              typeAheadDelay:250,//默认延时查询250     
              triggerAction : 'all', //每次下拉均显示全部选项       
              selectOnFocus:true,
              forceSelection: true,// 用户必须点击选择不能随意输入  
              typeAhead: true,// 自动提示并补充列出相似的选项  
              valueField : 'name', //下拉框显示信息对应的值     
              displayField : 'name', //下拉框显示信息     
              loadingText : '正在加载信息',      
              selectOnFocus : true, //用户不能自己输入,只能选择列表中有的记录     
              minChars:2,  //设置填充几个字节就去查一次,默认为4个字节      
              renderTo : 'sp_driver',
              listeners:{'select':function(combo,record,index){
            	   $("#hid_driver_id").val(record.data.id);
            	   $("#hid_driver_name").val(record.data.name);
            	   $("#driver_idno").val(record.data.idno==null?'':record.data.idno);
            	   $("#driver_driverLiceseNum").val(record.data.driverLiceseNum==null?'':record.data.driverLiceseNum)
            	   $("#driver_superCargoNum").val(record.data.superCargoNum==null?'':record.data.superCargoNum);
            	   $("#driver_contractPhone").val(record.data.contractPhone==null?'':record.data.contractPhone);
            	   if($.trim(record.data.photo)!=''){
            		   $("#driver_photo").attr('src',cfg.cxt+'/upload/'+record.data.photo);
            	   }
             }
    	  }
    	  });
	     $("#comb_driver").attr('datatype',"*");
	      //编辑时设置下拉框的默认值
	      if($("#hid_driver_name").val()!=''){
	    	  $("#comb_driver").val($("#hid_driver_name").val());//下拉框设置值
	      }
    	 
	      //押运员下拉框初始化
	      new Ext.form.ComboBox({ //默认过滤参数名为query     
              store: shipperForm.createEscortStore(),    
              hiddenName:'name',
              id:'comb_escort',
              width : 200,
              mode : 'local', //设置为local,则加载的本地数据     
              typeAheadDelay:250,//默认延时查询250     
              triggerAction : 'all', //每次下拉均显示全部选项       
              selectOnFocus:true,
              forceSelection: true,// 用户必须点击选择不能随意输入  
              typeAhead: true,// 自动提示并补充列出相似的选项  
              valueField : 'name', //下拉框显示信息对应的值     
              displayField : 'name', //下拉框显示信息     
              loadingText : '正在加载信息',      
              selectOnFocus : true, //用户不能自己输入,只能选择列表中有的记录     
              minChars:2,  //设置填充几个字节就去查一次,默认为4个字节      
              renderTo : 'sp_escort',
              listeners:{'select':function(combo,record,index){
            	  $("#hid_escort_id").val(record.data.id);
            	   $("#hid_escort_name").val(record.data.name);
            	   $("#escort_idno").val(record.data.idno==null?'':record.data.idno);
            	   $("#escort_superCargoNum").val(record.data.superCargoNum==null?'':record.data.superCargoNum);
            	   $("#escort_contractPhone").val(record.data.contractPhone==null?'':record.data.contractPhone);
            	   if($.trim(record.data.photo)!=''){
            		   $("#escort_photo").attr('src',cfg.cxt+'/upload/'+record.data.photo);
            	   }

             }
    	  }
    	  });
	     $("#comb_escort").attr('datatype',"*");
	      //编辑时设置下拉框的默认值
	      if($("#hid_escort_name").val()!=''){
	    	  $("#comb_escort").val($("#hid_escort_name").val());//下拉框设置值
	      }
      }
   }
}();
Ext.onReady(shipperForm.init, shipperForm);

(function($){
	/**添加表格元素**/
	add=function(){
		var _len = $("#goods_Tab tr").length-1;
		var _index=_len-1;
		var html='<tr id="'+_len+'">';
		html+='<td class="table-td"   width="15%">';
		html+='<input type="hidden" id="goodsId_'+_len+'" name="shipOrderGoods['+_index+'].goods.id" value="" />';
		html+='<input type="hidden" id="hid_goodsName_'+_len+'"   value="" /> ';
		html+='<span id="sp_goods_'+_len+'"/>';
		html+='</td>';
		html+='<td class="table-td"  width="15%">';
		html+='<input type="text"   class="input-text" id="internatCode_'+_len+'" name="internatCode"  style="width:80%;"   maxlength="10" readonly="readonly"/>';
		html+='</td>';
		html+='<td class="table-td"  width="15%">';
		html+='<input type="text"  class="input-text" id="code_'+_len+'" name="code"  style="width:80%;"  maxlength="10"  ignore="ignore" readonly="readonly"/>';
		html+='</td>';
		html+='<td class="table-td"  width="10%">';
		html+='<input type="text"  class="input-text"  id="shipNum_'+_len+'" name="shipOrderGoods['+_index+'].shipNum"  style="width:60%;"  maxlength="10" />';
		html+='</td>';
		html+='<td class="table-td"  width="10%">';
		html+='<input type="text"　class="input-text" id="shipWeight_'+_len+'" name="shipOrderGoods['+_index+'].shipWeight"  style="width:60%;"  maxlength="10" datatype="floatNumTwo" ignore="ignore"/>';
		html+='</td>';
		html+='<td class="table-td"  width="10%">';
		html+='<input type="text" class="input-text" id="shipVolume_'+_len+'" name="shipOrderGoods['+_index+'].shipVolume"  style="width:60%;"   maxlength="10" datatype="floatNumTwo" ignore="ignore"/>';
		html+='</td>';
		html+='<td class="table-td"  width="10%">';
		html+='<input type="text"   class="input-text" id="shipFee_'+_len+'" name="shipOrderGoods['+_index+'].shipFee"  style="width:60%;"   maxlength="10" datatype="floatNumTwo" ignore="ignore"/>';
		html+='</td>';
		html+='<td class="table-td"  width="15%">';
		html+='<span id="safetCard_sp_'+_len+'">应急方案</span> ';
	   	html+='<span style="margin:5px;" ><a href="javascript:void(0);" onclick="javascript:del('+_len+');" style="color:red;font-size:16px; text-decoration:none;">-</a><span>';
		html+='</td>';
		html+='</tr>';
		$("#goods_Tab").append(html);
		new Ext.form.ComboBox({ //默认过滤参数名为query     
            store: shipperForm.createGoodsStore(),    
            hiddenName:'name',
            id:'goods_'+_len,
            width : 170,
            mode : 'local', //设置为local,则加载的本地数据     
            typeAheadDelay:250,//默认延时查询250     
            triggerAction : 'all', //每次下拉均显示全部选项       
            selectOnFocus:true,
            forceSelection: true,// 用户必须点击选择不能随意输入  
            typeAhead: true,// 自动提示并补充列出相似的选项  
            valueField : 'id', //下拉框显示信息对应的值     
            displayField : 'name', //下拉框显示信息     
            loadingText : '正在加载信息',      
            selectOnFocus : true, //用户不能自己输入,只能选择列表中有的记录     
            minChars:2,  //设置填充几个字节就去查一次,默认为4个字节      
            renderTo : 'sp_goods_'+_len,
            listeners:{'select':function(combo,record,index){
            	for(var i=0;i<_len-1;i++){
            		if(record.data.id==$("#goodsId_"+(i+1)).val()){
            			Ext.Mytip.msg('提示', '该货物类型已经存在！');
            			allow_save=false;
            			goods_trIndex.push((i+1));//tr序号从1开始
        				return;
            		}
            	}
            	allow_add=true;
          	   $("#goodsId_"+_len).val(record.data.id);
          	   $("#internatCode_"+_len).val(record.data["category.internatCode"]);
          	   $("#code_"+_len).val(record.data.code);
          	 var safetyCardObj=record.data.safetyCard;
          	 if($.trim(safetyCardObj.path)!=''){
      		   var html='<a href="javascript:void(0);" onclick="javascript:shipperForm.openSafetyCardView('+_len+',\''+safetyCardObj.path+'\')">应急方案</a><span id="sp_content_'+_len+'">';
      		   html+='<img id="safetyCard_photo_img_'+_len+'" src="'+cfg.cxt+'/upload/'+safetyCardObj.path+'" style="display:none; width:100%;height:auto;"/>';
      		   html+='<span id="safetyCard_attach_sp_'+_len+'" style="display:none;margin-top:20px;padding:15px;"/>';
      		   html+='</span>';
      		   if(safetyCardObj.infoPath!=null && safetyCardObj.infoPath!=''){
      			 html+='<span style="margin-left:5px;"><a href="'+cfg.cxt+'/upload/"'+safetyCardObj.infoPath+'" target="_blank">安全说明书</a></span>';
      		   }
      		   $("#safetCard_sp_"+_len).html(html);
          	 }
          }
  	  }
  	  });
	      $("#goods_"+_len).attr('datatype',"*");
	      //编辑时设置下拉框的默认值
	      if($("#goodsId_"+_len).val()!=''){
	    	  var goodsName=$("#hid_goodsName_"+_len).val();
	    	  $("#goods_"+_len).val(goodsName);//下拉框设置值
	      }
	}
	/**删除表格元素**/
	del=function(index){
		$("tr[id='"+index+"']").remove();//删除当前行
		if(goods_trIndex.length>0){
			for(var i=0;i<goods_trIndex.length;i++){
				goods_trIndex.remove(i);
			}
		}
		if(goods_trIndex.length==0){
			allow_save=true;//删除保存
		}
	}
	delSite=function(index,type){
		if(type==1){
			$("tr[id='receive_tr_"+index+"']").remove();//删除当前行
			if(receive_trIndex.length>0){
				for(var i=0;i<receive_trIndex.length;i++){
					receive_trIndex.remove(i);//删除当前存在相同的记录
				}
			}
		}else {
			$("tr[id='send_tr_"+index+"']").remove();//删除当前行
			if(send_trIndex.length>0){
				for(var i=0;i<send_trIndex.length;i++){
					send_trIndex.remove(i);//删除当前存在相同的记录
				}
			}
		}
		
	}
	addSite=function(type){
		var _len,_index;
		if(type==2){
			_len = $("#sendSite_Tab tr").length-1;
			_index=_len-1;
			var html='<tr id="send_tr_'+_len+'">';
			html+='<td class="table-td"   width="15%">';
			html+='<input type="hidden" id="sendSiteId_'+_len+'" name="shipOrderSendSites['+_index+'].site.id" value="" />';
			html+='<input type="hidden" id="hid_goodsName_'+_len+'"   value="" /> ';
			html+='<span id="sp_sendSite_'+_len+'"/>';
			html+='</td>';
			html+='<td colspan="2" class="table-td"  width="25%">';
			html+='<input type="text" id="sendProvinceName_'+_len+'"  class="input-text" style="width:23%;"   readonly="readonly"/>';
   	   		html+='<span style="margin: 5px 5px;">';
   	   		html+='<input type="text" id="sendCityName_'+_len+'" class="input-text" style="width:24%;"   readonly="readonly"/>';
   	   		html+='</span>';	
   	   		html+='<span style="margin:5px 5px;">';
   	   		html+='<input type="text" id="sendAreaName_'+_len+'" class="input-text" style="width:27%;"   readonly="readonly"/>';
   	   		html+='</span></td>';
	   	 	html+='<td colspan="2" class="table-td"   width="25%">';
		   	html+='<textArea id="loadAddress_'+_len+'" class="textarea" style="width: 90%;" name="shipOrderSendSites['+_index+'].address" maxlength="100" datatype="*"></textArea>';
		   	html+='</td>';
		   	html+='<td class="table-td" width="10%">';
		    html+='<input id="send_contractor_'+_len+'" type="text"	style="width:90%;" class="input-text" name="shipOrderSendSites['+_index+'].contractor" maxlength="16" datatype="*" />';
		   	html+='</td>';
		   	html+='<td class="table-td" width="15%">';
		   	html+='<input id="send_contractPhone_'+_len+'" type="text"	style="width:90%" class="input-text" name="shipOrderSendSites['+_index+'].contractPhone" maxlength="16" datatype="*"/>';
		   	html+='</td>';
		   	html+='<td class="table-td" width="10%">';
		   	html+='<span id="send_photo_sp_'+_len+'"></span>';
		   	html+='<span style="margin:5px;" ><a href="javascript:void(0);" onclick="javascript:delSite('+_len+'\,2);" style="color:red;font-size:16px; text-decoration:none;">-</a><span>';
		   	html+='</td>';
			html+='</tr>';
			$("#sendSite_Tab").append(html);
			new Ext.form.ComboBox({ //默认过滤参数名为query     
	              store: shipperForm.createSiteStore(2),    
	              hiddenName:'name',
	              id:'sendSite_'+_len,
	              width : 170,
	              mode : 'local', //设置为local,则加载的本地数据     
	              typeAheadDelay:250,//默认延时查询250     
	              triggerAction : 'all', //每次下拉均显示全部选项       
	              selectOnFocus:true,
	              forceSelection: true,// 用户必须点击选择不能随意输入  
	              typeAhead: true,// 自动提示并补充列出相似的选项  
	              valueField : 'id', //下拉框显示信息对应的值     
	              displayField : 'name', //下拉框显示信息     
	              loadingText : '正在加载信息',      
	              selectOnFocus : true, //用户不能自己输入,只能选择列表中有的记录     
	              minChars:2,  //设置填充几个字节就去查一次,默认为4个字节      
	              renderTo : 'sp_sendSite_'+_len,
	              listeners:{'select':function(combo,record,index){
	            	 for(var i=0;i<_len-1;i++){
	              		if(record.data.id==$("#sendSiteId_"+(i+1)).val()){
	              			Ext.Mytip.msg('提示', '该发货站点已经存在！');
	              			allow_send_save=false;
	              			send_trIndex.push((i+1));//tr序号从1开始
	          				return;
	              		}
		           }
            	   $("#sendSiteId_"+_len).val(record.data.id);
            	   $("#hid_sendSiteName_"+_len).val(record.data.name);
            	   $("#sendProvinceName_"+_len).val(record.data.provinceName);
            	   $("#sendCityName_"+_len).val(record.data.cityName);
            	   $("#sendAreaName_"+_len).val(record.data.areaName);
            	   $("#send_contractor_"+_len).val(record.data.contractor);
            	   $("#send_contractPhone_"+_len).val(record.data.contractPhone);
            	   $("#loadAddress_"+_len).val(record.data.address);
            	   if($.trim(record.data.contractPhoto)!=''){
            		   var html='<a href="javascript:void(0);" onclick="javascript:shipperForm.viewPhoto(\''+record.data.contractPhoto+'\',\'send_photo_img_'+_len+'\')"> 查看照片</a>';
            		   html+='<img id="send_photo_img_'+_len+'" src="'+cfg.cxt+'/upload/'+record.data.contractPhoto+'" style="display:none;"/>';
            		   $("#send_photo_sp_"+_len).html(html);
            	   }
	             }
	    	  }
	    	  });
		      $("#sendSite_"+_len).attr('datatype',"*");
		}else{
			_len = $("#receiveSite_Tab tr").length-1;
			_index=_len-1;	
			var html='<tr id="receive_tr_'+_len+'">';
			html+='<td class="table-td"   width="15%">';
			html+='<input type="hidden" id="receiveSiteId_'+_len+'" name="shipOrderReceiveSites['+_index+'].site.id" value="" />';
			html+='<input type="hidden" id="hid_goodsName_'+_len+'"   value="" /> ';
			html+='<span id="sp_receiveSite_'+_len+'"/>';
			html+='</td>';
			html+='<td colspan="2" class="table-td"  width="25%">';
			html+='<input type="text" id="receiveProvinceName_'+_len+'"  class="input-text" style="width:23%;"   readonly="readonly"/>';
   	   		html+='<span style="margin: 5px 5px;">';
   	   		html+='<input type="text" id="receiveCityName_'+_len+'" class="input-text" style="width:24%;"   readonly="readonly"/>';
   	   		html+='</span>';	
   	   		html+='<span style="margin:5px 5px;">';
   	   		html+='<input type="text" id="receiveAreaName_'+_len+'" class="input-text" style="width:27%"   readonly="readonly"/>';
   	   		html+='</span></td>';
	   	 	html+='<td colspan="2" class="table-td"   width="25%">';
		   	html+='<textArea id="unloadAddress_'+_len+'" class="textarea" style="width: 90%;" name="shipOrderReceiveSites['+_index+'].address" maxlength="100" datatype="*"></textArea>';
		   	html+='</td>';
		   	html+='<td class="table-td" width="10%">';
		    html+='<input id="receive_contractor_'+_len+'" type="text"	style="width:90%;" class="input-text" name="shipOrderReceiveSites['+_index+'].contractor" maxlength="16" datatype="*" />';
		   	html+='</td>';
		   	html+='<td class="table-td" width="15%">';
		   	html+='<input id="receive_contractPhone_'+_len+'" type="text"	style="width:90%" class="input-text" name="shipOrderReceiveSites['+_index+'].contractPhone" maxlength="16" datatype="*"/>';
		   	html+='</td>';
		   	html+='<td class="table-td" width="10%">';
		   	html+='<span id="receive_photo_sp_'+_len+'"></span>';
			html+='<span style="margin:5px;" ><a href="javascript:void(0);" onclick="javascript:delSite('+_len+'\,1);" style="color:red;font-size:16px; text-decoration:none;">-</a><span>';
		   	html+='</td>';
			html+='</tr>';
			$("#receiveSite_Tab").append(html);
			new Ext.form.ComboBox({ //默认过滤参数名为query     
	              store: shipperForm.createSiteStore(1),    
	              hiddenName:'name',
	              id:'receiveSite_'+_len,
	              width : 170,
	              mode : 'local', //设置为local,则加载的本地数据     
	              typeAheadDelay:250,//默认延时查询250     
	              triggerAction : 'all', //每次下拉均显示全部选项       
	              selectOnFocus:true,
	              forceSelection: true,// 用户必须点击选择不能随意输入  
	              typeAhead: true,// 自动提示并补充列出相似的选项  
	              valueField : 'id', //下拉框显示信息对应的值     
	              displayField : 'name', //下拉框显示信息     
	              loadingText : '正在加载信息',      
	              selectOnFocus : true, //用户不能自己输入,只能选择列表中有的记录     
	              minChars:2,  //设置填充几个字节就去查一次,默认为4个字节      
	              renderTo : 'sp_receiveSite_'+_len,
	              listeners:{'select':function(combo,record,index){
	            	  for(var i=0;i<_len-1;i++){
		              		if(record.data.id==$("#receiveSiteId_"+(i+1)).val()){
		              			Ext.Mytip.msg('提示', '该收货站点已经存在！');
		              			allow_receive_save=false;
		              			receive_trIndex.push((i+1));//tr序号从1开始
		          				return;
		              		}
			           }
	            	   $("#receiveSiteId_"+_len).val(record.data.id);
	            	   $("#hid_receiveSiteName_"+_len).val(record.data.name);
	            	   $("#receiveProvinceName_"+_len).val(record.data.provinceName);
	            	   $("#receiveCityName_"+_len).val(record.data.cityName);
	            	   $("#receiveAreaName_"+_len).val(record.data.areaName);
	            	   $("#receive_contractor_"+_len).val(record.data.contractor);
	            	   $("#receive_contractPhone_"+_len).val(record.data.contractPhone);
	            	   $("#loadAddress_"+_len).val(record.data.address);
	            	   if($.trim(record.data.contractPhoto)!=''){
	            		   var html='<a href="javascript:void(0);" onclick="javascript:shipperForm.viewPhoto(\''+record.data.contractPhoto+'\',\'receive_photo_img_'+_len+'\')"> 查看照片</a>';
	            		   html+='<img id="receive_photo_img_'+_len+'" src="'+cfg.cxt+'/upload/'+record.data.contractPhoto+'" style="display:none;"/>';
	            		   $("#receive_photo_sp_"+_len).html(html);
	            	   }
	             }
	    	  }
	    	  });
		      $("#receiveSite_"+_len).attr('datatype',"*");
		}
		
	}
})(jQuery)


Array.prototype.remove=function(dx)
{
if(isNaN(dx)||dx>this.length){return false;}
for(var i=0,n=0;i<this.length;i++)
{
if(this[i]!=this[dx])
{
this[n++]=this[i]
}
}
this.length-=1
　}
 